package com.santoker.user.service.controller;

import com.santoker.baseservice.ApiResponseBody;
import com.santoker.user.service.dto.vo.DelBodyVo;
import com.santoker.user.service.dto.vo.DelListBodyVo;
import com.santoker.user.service.dto.vo.record.*;
import com.santoker.user.service.dto.vo.share.SharedDataVo;
import com.santoker.user.service.service.biz.record.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@Api(tags = "记录相关api")
@RestController
@RequestMapping("/painters/apis")
public class RecordController extends BaseController {

    @Autowired
    private RecordService recordService;

    @ApiOperation(value = "单个用户记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, paramType = "query", dataType = "Integer")})
    @GetMapping(value = {"/get_list"})
    public ApiResponseBody<List<UserRecordVo>> goodsList(
            @RequestParam(value = "userId", required = true) Integer userId
    ) {
        List<UserRecordVo> userRecordList = recordService.getUserRecordList(userId);
        return ApiResponseBody.createSuccessBody(userRecordList);
    }

    @ApiOperation(value = "单个用户分享列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", required = true, paramType = "query", dataType = "Integer")})
    @GetMapping(value = {"/get_share_list"})
    public ApiResponseBody<List<UserRecordVo>> shareList(
            @RequestParam(value = "userId", required = true) Integer userId
    ) {
        List<UserRecordVo> userShareList = recordService.getUserShareList(userId);
        return ApiResponseBody.createSuccessBody(userShareList);
    }

    @ApiOperation(value = "单个分享记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "recordId", required = true, paramType = "query", dataType = "Integer")})
    @GetMapping(value = {"/get_record"})
    public ApiResponseBody<SharedDataVo> share(
            @RequestParam(value = "recordId", required = true) Integer recordId
    ) {
        SharedDataVo sharedDataVo = recordService.getSharedData(recordId);
        return ApiResponseBody.createSuccessBody(sharedDataVo);
    }

    @ApiOperation(value = "分享、精选用户列表")
    @ApiImplicitParams({})
    @GetMapping(value = {"/get_user_list_select"})
    public ApiResponseBody<List<UserVo>> userListSelect() {
        List<UserVo> userList = recordService.getUserListShareAndSelect();
        return ApiResponseBody.createSuccessBody(userList);
    }

    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({})
    @GetMapping(value = {"/get_all_user_list"})
    public ApiResponseBody<List<UserVo>> getAllUser() {
        List<UserVo> userList = recordService.getAllUser();
        return ApiResponseBody.createSuccessBody(userList);
    }

    @ApiOperation(value = "姓名搜索用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "userName", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/search_user_by_name"})
    public ApiResponseBody<List<UserVo>> searchUserByName(
            @RequestParam(value = "userName", required = true) String userName
    ) {
        List<UserVo> userList = recordService.searchUserByName(userName);
        return ApiResponseBody.createSuccessBody(userList);
    }

    @ApiOperation(value = "手机号搜索用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "mobile", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/search_user_by_mobile"})
    public ApiResponseBody<List<UserVo>> searchUserByMobile(
            @RequestParam(value = "mobile", required = true) String mobile
    ) {
        List<UserVo> userList = recordService.searchUserByMobile(mobile);
        return ApiResponseBody.createSuccessBody(userList);
    }

    @ApiOperation(value = "搜索曲线")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "title", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "searchType", value = "searchType", required = false, paramType = "query", dataType = "int")})
    @GetMapping(value = {"/search_record"})
    public ApiResponseBody<List<UserRecordVo>> searchRecord(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "searchType", required = false) int searchType
    ) {
        List<UserRecordVo> userRecordList = recordService.getUserRecordListByTitle(title, searchType);
        return ApiResponseBody.createSuccessBody(userRecordList);
    }

    @ApiOperation(value = "所有曲线")
    @ApiImplicitParams({})
    @GetMapping(value = {"/all_record"})
    public ApiResponseBody<List<UserRecordVo>> recordAll() {
        List<UserRecordVo> userRecordList = recordService.getAllRecord();
        return ApiResponseBody.createSuccessBody(userRecordList);
    }

    @ApiOperation(value = "获取曲线列表", notes = "包含精选曲线，分享曲线，训练集曲线")
    @ApiImplicitParams({})
    @PostMapping(value = {"/get_record_list"})
    public ApiResponseBody<List<UserRecordVo>> getRecordList(
            @Valid @RequestBody RecordListVo body
    ) {
        List<UserRecordVo> userRecordList = recordService.getRecordList(body);
        return ApiResponseBody.createSuccessBody(userRecordList);
    }

    @ApiOperation(value = "所有训练集曲线")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "type", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/all_train_record"})
    public ApiResponseBody<List<UserRecordVo>> recordAllTrain(
            @RequestParam(value = "type", required = true) String type
    ) {
        List<UserRecordVo> userRecordList = recordService.getAllTrainRecord(type);
        return ApiResponseBody.createSuccessBody(userRecordList);
    }

    @ApiOperation(value = "分享用户列表")
    @ApiImplicitParams({})
    @GetMapping(value = {"/get_user_list"})
    public ApiResponseBody<List<UserVo>> userList() {
        List<UserVo> userList = recordService.getUserListShare();
        return ApiResponseBody.createSuccessBody(userList);
    }

    @ApiOperation(value = "增加记录信息")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_record"})
    public ApiResponseBody add(
            @Valid @RequestBody RecordAddVo body, BindingResult result
    ) {
        validParams(result);

        Integer recordId = recordService.addRecord(body);
        return ApiResponseBody.createSuccessBody(recordId);
    }

    @ApiOperation(value = "更新记录信息")
    @ApiImplicitParams({})
    @PostMapping(value = {"/update_record"})
    public ApiResponseBody update(
            @Valid @RequestBody RecordUpdateVo body, BindingResult result
    ) {
        validParams(result);

        recordService.updateRecord(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "更新用户曲线的title与type")
    @ApiImplicitParams({})
    @PostMapping(value = {"/update_user_record"})
    public ApiResponseBody updateUserRecord(
            @Valid @RequestBody RecordModVo body, BindingResult result
    ) {
        validParams(result);

        recordService.updateUserRecord(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "设置是否为精选曲线")
    @ApiImplicitParams({})
    @PostMapping(value = {"/set_is_selected"})
    public ApiResponseBody setIsSelect(
            @Valid @RequestBody RecordIdVo body, BindingResult result
    ) {
        validParams(result);

        recordService.setIsSelect(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "设置是否为训练集")
    @ApiImplicitParams({})
    @PostMapping(value = {"/set_is_train"})
    public ApiResponseBody setIsTrain(
            @Valid @RequestBody RecordIdVo body, BindingResult result
    ) {
        validParams(result);

        recordService.setIsTrain(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "批量删除训练集")
    @ApiImplicitParams({})
    @PostMapping(value = {"/del_train_list"})
    public ApiResponseBody delTrainList(
            @Valid @RequestBody DelListBodyVo body
    ) {
        recordService.delTrainList(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "设置是否为分享")
    @ApiImplicitParams({})
    @PostMapping(value = {"/add_share"})
    public ApiResponseBody addShare(
            @Valid @RequestBody RecordIdVo body, BindingResult result
    ) {
        validParams(result);

        recordService.setIsShare(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "删除记录信息")
    @ApiImplicitParams({})
    @PostMapping(value = {"/del_record"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBodyVo body, BindingResult result
    ) {
        validParams(result);

        recordService.delRecord(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "删除所有记录信息")
    @ApiImplicitParams({})
    @PostMapping(value = {"/del_all"})
    public ApiResponseBody delAll(
            @Valid @RequestBody DelBodyVo body, BindingResult result
    ) {
        validParams(result);

        recordService.delAllRecord(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "删除分享记录信息")
    @ApiImplicitParams({})
    @PostMapping(value = {"/del_share"})
    public ApiResponseBody delShare(
            @Valid @RequestBody DelBodyVo body, BindingResult result
    ) {
        validParams(result);

        recordService.delShare(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "版本信息")
    @ApiImplicitParams({})
    @GetMapping(value = {"/get_version"})
    public ApiResponseBody<String> getVersion() {
        return ApiResponseBody.createSuccessBody("9.27.0 | 11.15.0  | 12.30.0");
    }

    @ApiOperation(value = "计算火力")
    @ApiImplicitParams({})
    @PostMapping(value = {"/calc_firepower"})
    public ApiResponseBody calcFirepower(
            @Valid @RequestBody CalcFirepowerVo body, BindingResult result
    ) {
        validParams(result);

        List<BigDecimal> fpList = recordService.calcFirepower(body);
        return ApiResponseBody.createSuccessBody(fpList);
    }

    @ApiOperation(value = "批量设置分享")
    @ApiImplicitParams({})
    @PostMapping(value = {"/batch_set_share"})
    public ApiResponseBody batchSetShare(
            @Valid @RequestBody RecordIdListVo body, BindingResult result
    ) {
        validParams(result);

        recordService.batchSetShare(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "批量删除记录")
    @ApiImplicitParams({})
    @PostMapping(value = {"/batch_del_record"})
    public ApiResponseBody batchDelRecord(
            @Valid @RequestBody RecordIdListDelVo body, BindingResult result
    ) {
        validParams(result);

        recordService.batchDelRecord(body);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "获取曲线记录口令")
    @ApiImplicitParams({@ApiImplicitParam(name = "recordId", value = "recordId", required = true, paramType = "query", dataType = "Integer")})
    @GetMapping(value = {"/get_record_token"})
    public ApiResponseBody<String> getRecordToken(
            @RequestParam(value = "recordId", required = true) Integer recordId
    ) {
        String recordToken = recordService.getRecordToken(recordId);
        return ApiResponseBody.createSuccessBody(recordToken);
    }

    @ApiOperation(value = "根据口令，获取曲线记录")
    @ApiImplicitParams({@ApiImplicitParam(name = "recordToken", value = "recordToken", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_record_by_record_token"})
    public ApiResponseBody<UserRecordVo> getRecordByRecordToken(
            @RequestParam(value = "recordToken", required = true) String recordToken
    ) {
        UserRecordVo userRecord = recordService.getRecordByRecordToken(recordToken);
        return ApiResponseBody.createSuccessBody(userRecord);
    }

    @ApiOperation(value = "设置置顶/取消置顶")
    @ApiImplicitParams({})
    @PostMapping(value = {"/record/isTop"})
    public ApiResponseBody recordIsTop(
            @Valid @RequestBody RecordIdVo body, BindingResult result
    ) {
        validParams(result);

        recordService.setIsTop(body);
        return ApiResponseBody.createSuccessBody();
    }

}
